package com.epoint.mobileoa.tools;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.os.Environment;

public class SDCard {
	// 得到图片名称
	public static String getName(String path) {
		int lastSeparatorIndex = path.lastIndexOf("/");
		return path.substring(lastSeparatorIndex + 1, path.length());
	}

	// 获得SDCard下所有图片的路径
	public static List<String> getImagePathFromSDCard(String path) {
		List<String> imagePaths = new ArrayList<String>();
		try {
			File file = new File(path);
			File[] files = file.listFiles();
			for (File theFile : files) {
				if (isImageFile(theFile.getPath())) {
					imagePaths.add(theFile.getPath());
				}
			}
		} catch (Exception e) {
			return imagePaths;
		}
		return imagePaths;
	}

	// 获得SDCard下所有JPG格式图片的路径
	public static List<String> getJpgImagePathFromSDCard(String path) {
		List<String> imagePaths = new ArrayList<String>();
		try {
			File file = new File(path);
			File[] files = file.listFiles();
			for (File theFile : files) {
				if (isJPGImageFile(theFile.getPath())) {
					// 判断是不是加密的图片
					if (isJaMiImage(theFile)) {
						imagePaths.add(theFile.getPath());
					}
				}
			}
		} catch (Exception e) {
			return imagePaths;
		}
		return imagePaths;
	}

	// 判断是不是加密过的图片
	public static boolean isJaMiImage(File theFile) {
		String filename = SDCard.getName(theFile.getPath());
		// 获得文件名中的密码位
		String temp[] = filename.split(" ");
		// temp[2]=e35647.jpg]
		String temp3 = temp[2].substring(0, 6);
		// 解密
		String jiemi = MiMa.jiemi(temp3);
		// 获得比较位
		String index2 = jiemi.substring(1, 5);
		// 获得文件名中的密码生成位
		String temp2[] = temp[1].split("-");
		// 获得比较位
		String index = temp2[1] + temp2[2];
		// 判断是否匹配
		if (index.equals(index2) && temp3.length() == 6) {
			return true;
		}
		return false;
	}

	// 判断是否有内存卡
	public static boolean hasSDcard() {
		if (Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED)) { // sd卡存在
			return true;
		}
		return false;
	}

	// 获得地图文件的路径
	public static String getMapFilePath(String foldername) {
		// 获取根目录
		File sdDir = Environment.getExternalStorageDirectory();
		// return sdDir.toString() + "/" + foldername + "/" + foldername +
		// ".map";
		return sdDir.toString() + "/cscgtmap/" + foldername + ".map";
	}
	
	// 判断地图文件是否存在
	public static boolean hasMapFile(String foldername) {
		boolean has = false;
		File sdDir = Environment.getExternalStorageDirectory();
		File file = new File(sdDir.toString() + "/cscgtmap/" + foldername
				+ ".map");
		if (file.isFile()) {
			has = true;

		}
		return has;
	}

	// 获取文件保存路径
	public static File getFileSavePath(String savepath) {
		File filePath = null;
		if (hasSDcard()) {
			filePath = new File(savepath);
			if (!filePath.isDirectory()) {// 判断文件存在与否，不存在就创建
				filePath.mkdirs();
			}
			return filePath;
		} else {
			return null;
		}
	}

	// 判断是否是图片文件
	public static boolean isImageFile(String fileName) {
		String extension = fileName.substring(fileName.lastIndexOf(".") + 1,
				fileName.length()).toLowerCase();
		if (extension.equals("jpg") || extension.equals("png")
				|| extension.equals("gif") || extension.equals("jpeg")
				|| extension.equals("bmp")) {
			return true;
		}
		return false;
	}

	// 判断是否是JPG图片文件
	public static boolean isJPGImageFile(String fileName) {
		String extension = fileName.substring(fileName.lastIndexOf(".") + 1,
				fileName.length()).toLowerCase();
		if (extension.equals("jpg")) {
			return true;
		}
		return false;
	}

	public static boolean deleteFile(String path) {
		File file = new File(path);
		return file.delete();
	}

}
